//
//  ConnectViewController.m
//  NekaPrice
//
//  Created by yu on 2018/6/27.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "ConnectViewController.h"
#import "AlertSelectView.h"
#import <WebKit/WebKit.h>
@interface ConnectViewController ()<WKNavigationDelegate, UIScrollViewDelegate>
@property (nonatomic, strong)AlertSelectView *alertSelectView;
@property (nonatomic, assign)CGFloat alertWidth;
@property (nonatomic, strong)WKWebView *webView;
@property (nonatomic, strong)UIButton *telBtn;
@property (nonatomic, strong)NSArray *URLs;
@end

@implementation ConnectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self hideNavBar];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showOrHideAlertSelectView) name:@"SELECT_CONNECT_VIEWCONTROLLER" object:nil];
    
    _URLs = @[@"http://neka.com.cn/gzh/wyjm/",
              @"http://gzh.neka.com.cn/gzh/mdcx/",
              @"http://ewm.neka.com.cn/search_zb.php",
              @"tel:4006914006"];
//    //背景图
//    UIImageView *bgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
//    bgView.image = [UIImage imageNamed:@"body"];
//    [self.view addSubview:bgView];
//    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.bottom.mas_equalTo(0);
//        make.bottom.mas_equalTo(-bottom);
//    }];
    
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.top);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-self.bottom);
    }];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_URLs[0]]]];
    
    
    
    [self.view addSubview:self.alertSelectView];
    [self hideAlertViewWithAnimated:NO];
    
    
    
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
    [self showHUD];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    [self hideHUD];
}



- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self showAlertViewWithAnimated:YES];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self hideAlertViewWithAnimated:NO];
}


- (WKWebView *)webView {
    if (_webView == nil) {
        _webView = [[WKWebView alloc] init];
        _webView.backgroundColor = [UIColor clearColor];
        _webView.navigationDelegate = self;
        _webView.scrollView.delegate = self;
    }
    return _webView;
}


- (AlertSelectView *)alertSelectView {
    if (_alertSelectView == nil) {
        _alertSelectView = [[AlertSelectView alloc] initWithFrame:CGRectMake(K_WIDTH - self.alertWidth - 10, K_HEIGHT - 180 - self.bottom, self.alertWidth, 160)];
        _alertSelectView.show = NO;
        __weak typeof(self) weakSelf = self;
        _alertSelectView.selectedBlock = ^(NSInteger index) {
            [weakSelf hideAlertViewWithAnimated:YES];
            if (index == 3) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:weakSelf.URLs[index]]];
            }else {
                [weakSelf.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:weakSelf.URLs[index]]]];
            }
        };
        
    }
    return _alertSelectView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self hideAlertViewWithAnimated:YES];
    [self.view endEditing:YES]; 
}

//显示或隐藏选择框
- (void)showOrHideAlertSelectView {
    
    if (self.alertSelectView.show) {//隐藏
        [self hideAlertViewWithAnimated:YES];
    }else {//显示
        [self showAlertViewWithAnimated:YES];
    }
    
}

//显示
- (void)showAlertViewWithAnimated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.3 animations:^{
            self.alertSelectView.frame = CGRectMake(K_WIDTH - self.alertWidth - 10, K_HEIGHT - 180 - self.bottom, self.alertWidth, 160);
        } completion:^(BOOL finished) {
            self.alertSelectView.show = YES;
        }];
    }else {
        self.alertSelectView.frame = CGRectMake(K_WIDTH - self.alertWidth - 10, K_HEIGHT - 180 - self.bottom, self.alertWidth, 160);
        self.alertSelectView.show = YES;
    }
    
}

//隐藏
- (void)hideAlertViewWithAnimated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.3 animations:^{
            self.alertSelectView.frame = CGRectMake(K_WIDTH - self.alertWidth - 10, K_HEIGHT, self.alertWidth, 160);
        } completion:^(BOOL finished) {
            self.alertSelectView.show = NO;
        }];
    }else {
        self.alertSelectView.frame = CGRectMake(K_WIDTH - self.alertWidth - 10, K_HEIGHT, self.alertWidth, 160);
        self.alertSelectView.show = NO;
    }
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self hideAlertViewWithAnimated:YES];
}



#pragma mark -- alertView的宽度

- (CGFloat)alertWidth {
    if (_alertWidth == 0) {
        NSString *str = @"我要加盟";
        CGRect rect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
        _alertWidth = rect.size.width + 40;
    }
    return _alertWidth;
}

@end
